import { h, ref, computed, reactive, onMounted } from "vue";
import { type PaginationProps } from "@pureadmin/table";
import { ElDatePicker, ElMessage, ElMessageBox, FormInstance } from "element-plus";
import {
  SecuritiesPortfolioGetPurchasedPortfolioData,
  SecuritiesPortfolioGetAnalysisReport
} from "@/api/stu/Cecurity";
import { useRoute, useRouter } from "vue-router";
export function usePortfolioAnalysis() {
  const [route, router] = [useRoute(), useRouter()];
  const loading = ref<boolean>(false)
  const InvestVisible = ref<boolean>(false)
  const dataList = ref();
  const Ttimes = ref();
  const portfolioldId = ref<any>("");
  const pagination = reactive<PaginationProps>({
    total: 0,
    pageSize: 10,
    currentPage: 1,
    pageSizes: [5, 10, 15],
    background: true
  });
  const columns: TableColumnList = [
    {
      label: "序号",
      type: "index",
      minWidth: 100
    }, {
      label: "投资组合名称",
      prop: "portfolioldName"
    }, {
      label: "客群名称",
      prop: "clientBaseName",
    }, {
      label: "模拟年化收益率(%)",
      prop: "annualYield"
    }, {
      label: "模拟年化波动率(%)",
      prop: "annualizedVolatility"
    },
    {
      label: "购买日期",
      prop: "purchaseTime"
    }, {
      label: "分析日期",
      prop: "parsingTimeByRoly"
    },
    {
      label: "操作",
      fixed: "right",
      width: 250,
      slot: "operation"
    }
  ];
  const dataLists = ref([]);
  const DialogVisible = ref(false)
  // TODO: 查看分析报告  -- 投资组合分析报告
  async function optimizeReport(row, nums) {
    let res = null;

    if (nums === 0) {
      const data = {
        "planId": route.query.planId,
        "taskId": route.query.taskId,
        portfolioId: row.portfolioldId,
        operationType: 1,
        analyseTime: null,
        type: "证券公司"
      };
      res = await SecuritiesPortfolioGetAnalysisReport(data);
      if (res.success) {
        DialogVisible.value = true;
        dataLists.value = [res?.data]
      } else {
        ElMessage.error(res.msg);
      }
    } else if (nums === 2) {
      InvestVisible.value = true;
      portfolioldId.value = row
      // ElMessageBox.alert(
      //   '',
      //   '请选择日期',
      //   {
      //     center: true,
      //     message: () =>
      //       h(ElDatePicker, {
      //         type: "date",
      //         modelValue: Ttimes.value,
      //         "onUpdate:modelValue": (val: string) => {
      //           Ttimes.value = val
      //         }
      //       })
      //   }).then(async () => {
      //     const data = {
      //       "planId": route.query.planId,
      //       "taskId": route.query.taskId,
      //       portfolioId: row.portfolioldId,
      //       operationType: 0,
      //       analyseTime: Ttimes.value,
      //       type: "证券公司"
      //     };
      //     res = await SecuritiesPortfolioGetAnalysisReport(data);
      //     if (res.success) {
      //       ElMessage.success(res.msg);
      //       GetDataList()
      //     } else {
      //       ElMessage.error(res.msg);
      //     }
      //   })
      //   .catch(() => {
      //     Ttimes.value = "";
      //   })

    }
  }

  // 确定投资
  async function footerinvest() {
    if (!Ttimes.value) {
      ElMessage.warning("请选择日期")
      return;
    }
    const data = {
      "planId": route.query.planId,
      "taskId": route.query.taskId,
      portfolioId: portfolioldId.value.portfolioldId,
      operationType: 0,
      analyseTime: Ttimes.value,
      type: "证券公司"
    };
    const res = await SecuritiesPortfolioGetAnalysisReport(data);
    if (res.success) {
      ElMessage.success(res.msg);
      GetDataList()
      closeinvest()
    } else {
      ElMessage.error(res.msg);
    }
  }

  // 取消投资
  function closeinvest() {
    InvestVisible.value = false;
    Ttimes.value = "";
    portfolioldId.value = "";
  }


  async function GetDataList() {
    const data = {
      pageIndex: pagination.currentPage,
      pageCount: pagination.pageSize,
      taskId: route.query.taskId,
      planId: route.query.planId,
    };
    const res = await SecuritiesPortfolioGetPurchasedPortfolioData(data);
    if (res.success) {
      dataList.value = res?.data?.items;
      pagination.total = res?.data?.totalCount;
      loading.value = false;
    } else {
      ElMessage.error(res.msg);
      setTimeout(() => { loading.value = false; }, 1500)
    }
  }

  const disabledDate = (time: Date) => {
    return time.getTime() < new Date(portfolioldId.value.purchaseTime).getTime()
  }

  function handleSizeChange() { GetDataList() }
  function handleCurrentChange() { GetDataList() }

  onMounted(() => {
    GetDataList()
  })

  return {
    Ttimes,
    loading,
    dataList,
    columns,
    pagination,
    dataLists,
    DialogVisible,
    InvestVisible,
    closeinvest,
    footerinvest,
    disabledDate,
    optimizeReport,
    handleSizeChange,
    handleCurrentChange
  }
}
